package com.yuxi.common.html;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import com.yuxi.common.FileConfig;
import com.yuxi.service.IBaseInfoService;
import com.yuxi.service.IProductTypeService;


import javax.annotation.Resource;
import java.io.IOException;

/**
 * 静态页面生成
 *
 * @author sunke
 * @version 1.0
 * @date 2021-07-19 10:25
 */

@Aspect
@Slf4j
@Component
public class SkCreateHtmlAspect {

    @Resource
    private SkCreateHtmlUtil skCreateHtmlUtil;

    @Autowired
    private FileConfig fileConfig;
    @Resource
    private IBaseInfoService baseInfoService;





    @Resource
    private IProductTypeService productTypeService;


    @Pointcut(value = "execution(* com.yuxi.controller.front..*.*(..)) ")
    private void aspectPointcut() {

    }

    @Before(value = "aspectPointcut()")
    public void aspectBefore(JoinPoint joinPoint) {
        SkContextModel skContextModel = findSkContextModel(joinPoint);
        if (skContextModel == null) {
            return;
        }
        Model model = findModel(joinPoint);
        if (model == null) {

            return;
        }


        skContextModel.setModel(model);


    }

    @After(value = "aspectPointcut()")
    public void aspectAfter(JoinPoint joinPoint) {

        SkContextModel skContextModel = findSkContextModel(joinPoint);
        String lang = null;


        if (skContextModel != null) {
            lang = skContextModel.getLang();
            skContextModel.addAttribute("website", fileConfig.getWebsite());
            skContextModel.addAttribute("lang", lang);
            skContextModel.addAttribute("company", baseInfoService.detail());

         //   skContextModel.addAttribute("productTypeList", productTypeService.getListBySizeAndLang(6,lang));


            try {
                if(skContextModel.isCreateHtmlFlag()) {




                    skCreateHtmlUtil.createHtmlList(skContextModel.getContext(), skContextModel.getTemplateName(), skContextModel.getHtmlList(), lang);
                }
            } catch (IOException e) {

            }

        }


    }


    private SkContextModel findSkContextModel(JoinPoint joinPoint) {
        if (joinPoint.getArgs() == null) {
            return null;
        }
        for (Object o : joinPoint.getArgs()) {
            if (o instanceof SkContextModel) {
                return (SkContextModel) o;
            }
        }
        return null;

    }

    private Model findModel(JoinPoint joinPoint) {
        if (joinPoint.getArgs() == null) {
            return null;
        }
        for (Object o : joinPoint.getArgs()) {
            if (o instanceof Model) {
                return (Model) o;
            }
        }
        return null;

    }


}
